﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;

namespace StoragePeriod.Web.UserControls
{
    public partial class Picker : SecurityPage
    {
        private string RoleCode
        {
            get
            {
                return Request["rolecode"];
            }
        }

        private string PickType
        {
            get
            {
                return Request["type"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
                LoadGridViewData();
        }

        protected void gvprivilegeView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvprivilegeView.PageIndex = e.NewPageIndex;
            LoadGridViewData();
        }

        private void LoadGridViewData()
        {
            if (RoleCode.IsNullOrEmpty())
            {
                Page.AlertAndClose("缺少参数");
            }

            switch (PickType)
            {
                case "privilege":
                    var currentlyprivileges = RolePrivileges.Where("RoleCode==@0", RoleCode).Select(p => p.PrivCode).ToList();
                    var privileges = from q in Privileges.All()
                                     where !currentlyprivileges.Contains(q.PrivCode)
                                     select new
                                     {
                                         编码 = q.PrivCode,
                                         权限名 = q.PrivName,
                                         权限描述 = q.PrivDesc
                                     };

                    gvprivilegeView.DataKeyNames = new string[] { "编码" };
                    gvprivilegeView.DataSource = privileges;
                    gvprivilegeView.DataBind();
                    break;
                case "user":

                    var currentlyUsers = RoleUsers.Where("RoleCode==@0", RoleCode).Select(p => p.UserName).ToList();
                    var users = from q in user.All()
                                where !currentlyUsers.Contains(q.UName)
                                select new
                                {
                                    编码 = q.id,
                                    用户名 = q.UName,
                                    联系方式 = q.Tel
                                };

                    gvprivilegeView.DataKeyNames = new string[] { "编码" };
                    gvprivilegeView.DataSource = users;
                    gvprivilegeView.DataBind();

                    break;
            }


        }

        protected void bu_post_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow item in gvprivilegeView.Rows)
            {
                CheckBox chose = item.FindControl("chose") as CheckBox;
                if (!chose.Checked) continue;
                string accesskey = chose.ToolTip;

                switch (PickType)
                {
                    case "privilege":

                        var newPriv = new RolePrivileges()
                        {
                            RoleCode = RoleCode,
                            PrivCode = accesskey,
                            PrivLineNo = -1
                        };
                        RolePrivileges.Insert(newPriv);
                        break;
                    case "user":

                        var res = item.Controls.AllOfType<DataControlFieldCell>();
                        var newUser = new RoleUsers()
                        {
                            RoleCode = RoleCode,
                            UserCode = accesskey,
                            RoleTypeCode = "1",
                            UserName = res.FirstOrDefault(p => p.ContainingField.HeaderText == "用户名").Text
                        };
                        RoleUsers.Insert(newUser);
                        break;
                }

                Page.AlertAndClose("设置成功");

                LoadGridViewData();
            }
        }


    }
}